package demo.virtuemart.base;

import java.io.FileInputStream;
import java.util.Hashtable;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.testng.Assert;

import demo.virtuemart.Util.ErrorUtil;
import demo.virtuemart.Util.xls_Reader;

public class TestBase {
	public static Properties props = null;
	public static Logger logs = null;
	public static Properties OR = null;
	public static xls_Reader xlsSuite = null;
	public static xls_Reader suite_category_xls = null;
	public static xls_Reader suite_cart_xls=null;
	public static xls_Reader suite_productdisplay_xls=null;
	public static boolean isInitialized=false;
	public static WebDriver web_driver=null;
	public static EventFiringWebDriver driver;
	public static boolean isBrowserOpened=false;
	public static Hashtable<String, String> sessionData = new Hashtable<String, String>();
	public void initialize() throws Throwable
	{
		if(!isInitialized)
		{
			logs = Logger.getLogger("devpinoyLogger");
			logs.debug("Loading Property files");
			
			props = new Properties();
			FileInputStream instream = new FileInputStream(System.getProperty("user.dir")+"//src//demo//virtuemart//config//config.properties");
			props.load(instream);
			
			OR = new Properties();
			FileInputStream instreamor = new FileInputStream(System.getProperty("user.dir")+"//src//demo//virtuemart//config//OR.properties");
			OR.load(instreamor);
			logs.debug("Loaded Property files successfully");
			
			
			xlsSuite = new xls_Reader(System.getProperty("user.dir")+"//src//demo//virtuemart//xslx//Suite.xlsx");
			suite_category_xls = new xls_Reader(System.getProperty("user.dir")+"//src//demo//virtuemart//xslx//Category Layout.xlsx");
			suite_cart_xls = new xls_Reader(System.getProperty("user.dir")+"//src//demo//virtuemart//xslx//Cart Suite.xlsx");
			suite_productdisplay_xls = new xls_Reader(System.getProperty("user.dir")+"//src//demo//virtuemart//xslx//Product Display Suite.xlsx");
			logs.debug("xls files successfully");
			isInitialized=true;
		}
	}
	public void openBrowser()
	{
		if(!isBrowserOpened)
		//Web_driver = new FirefoxDriver();
		{
				if(props.getProperty("browserType").equals("Firefox"))
				{
					
					web_driver = new FirefoxDriver();
					driver= new EventFiringWebDriver(web_driver);
				}
				else if(props.getProperty("browserType").equals("IE"))
				{
					web_driver = new InternetExplorerDriver();
					driver= new EventFiringWebDriver(web_driver);
					
				}
				else if(props.getProperty("browserType").equals("Chrome"))
				{
					System.setProperty("webdriver.chrome.driver",  "C:\\Manjusha\\Selenium\\ChromeDriver\\chromedriver.exe");
					web_driver = new ChromeDriver();
					driver= new EventFiringWebDriver(web_driver);
				}
				isBrowserOpened= true;
		}
		
	}
	public static void closeBrowser()
	{
		driver.close();
	}
	
	public boolean verifyTitle(String expectedValue)
	{
		try
		{
			String actualtitle= driver.getTitle();
			Assert.assertEquals(actualtitle, expectedValue);
			System.out.println("Title is matching");
		}
		catch(Throwable t)
		{
			ErrorUtil.addVerificationFailure(t);
			//fail=true;
			logs.debug("Title do not match");
		   return false;
		}
		return true;
	}
	
	public boolean compareNumbers(int expectedValue, int actualValue)
	{
		try
		{
			//String actualtitle= driver.getTitle();
			Assert.assertEquals(actualValue, expectedValue);
			System.out.println("Vaues are matching");
		}
		catch(Throwable t)
		{
			ErrorUtil.addVerificationFailure(t);
			//fail=true;
			logs.debug("Values doesn't match");
		   return false;
		}
		return true;
	}
	
	public boolean checkElementPresence(String xPathKey)
	{
			int elementNo= driver.findElements(By.xpath(OR.getProperty(xPathKey))).size();
			try
			{
			Assert.assertTrue(elementNo>0, "No element present");
			}
		catch(Throwable t)
		{
			ErrorUtil.addVerificationFailure(t);
			//fail=true;
			logs.debug("No element present");
		   return false;
		}
		return true;
	}
	
	public WebElement getObject(String xpathKey){
		
		try{
		WebElement x = driver.findElement(By.xpath(OR.getProperty(xpathKey)));
		return x;
		}catch(Throwable t){
			// report error
			ErrorUtil.addVerificationFailure(t);			
			logs.debug("Cannot find object with key -- " +xpathKey);
			return null;
		}
	}
	
	public void login(String userName, String pswd)
	{
		driver.findElement(By.xpath("//*[@id='modlgn-username']")).sendKeys(userName);
		driver.findElement(By.xpath("//*[@id='modlgn-passwd']")).sendKeys(pswd);
		driver.findElement(By.xpath("//*[@id='login-form']/fieldset/div/input")).click();
	}
}
